package leetcode.editor.day;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

// 655. 输出二叉树
// https://leetcode.cn/problems/print-binary-tree/
class PrintBinaryTree {
    public static void main(String[] args) {
        Solution solution = new PrintBinaryTree().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    // https://leetcode.cn/problems/print-binary-tree/solution/by-tong-zhu-yq3l/
    class Solution {
        // DFS
        /*LinkedList<List<String>> res = new LinkedList<>();

        public List<List<String>> printTree(TreeNode root) {
            // 注意是高度
            int h = getDepth(root) - 1;
            int m = h + 1;
            int n = (1 << (h + 1)) - 1;
            for (int i = 0; i < m; i++) {
                LinkedList<String> list = new LinkedList<>();
                for (int j = 0; j < n; j++) {
                    list.add("");
                }
                res.add(list);
            }


            dfs(root, 0, n - 1, 0);
            return res;
        }

        public int getDepth(TreeNode root) {
            if (root == null) return 0;
            return 1 + Math.max(getDepth(root.left), getDepth(root.right));
        }

        public void dfs(TreeNode root, int l, int r, int d) {
            if (root == null) return;

            int m = l + (r - l) / 2;
            // 使用set，add会进行添加
            res.get(d).set(m, String.valueOf(root.val));

            if (root.left != null) dfs(root.left, l, m - 1, d + 1);
            if (root.right != null) dfs(root.right, m + 1, r, d + 1);
        }*/

        // BFS
        LinkedList<List<String>> res = new LinkedList<>();

        public List<List<String>> printTree(TreeNode root) {
            // 注意是高度
            int h = getDepth(root) - 1;
            int m = h + 1;
            int n = (1 << (h + 1)) - 1;
            for (int i = 0; i < m; i++) {
                LinkedList<String> list = new LinkedList<>();
                for (int j = 0; j < n; j++) {
                    list.add("");
                }
                res.add(list);
            }


            bfs(new MyNode(root, 0, n - 1));
            return res;
        }

        private void bfs(MyNode root) {
            Queue<MyNode> queue = new LinkedList<>();
            queue.add(root);
            int h = 0;

            while (!queue.isEmpty()) {
                int size = queue.size();

                for (int i = 0; i < size; i++) {
                    MyNode myNode = queue.poll();
                    TreeNode node = myNode.node;
                    int l = myNode.l;
                    int r = myNode.r;

                    int m = l + ((r - l) >> 1);
                    res.get(h).set(m, String.valueOf(node.val));

                    if (myNode.node.left != null) {
                        queue.add(new MyNode(node.left, l, m - 1));
                    }

                    if (myNode.node.right != null) {
                        queue.add(new MyNode(node.right, m + 1, r));
                    }

                }

                h++;
            }
        }

        public int getDepth(TreeNode root) {
            if (root == null) return 0;
            return 1 + Math.max(getDepth(root.left), getDepth(root.right));
        }


        class MyNode {
            TreeNode node;
            int l;
            int r;

            public MyNode(TreeNode node, int l, int r) {
                this.node = node;
                this.l = l;
                this.r = r;
            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
